/*//GEN-FIRST:event_jButton1MouseClicked
 * Settings.java//GEN-LAST:event_jButton1MouseClicked
 *
 * Created on December 12, 2009, 11:17 PM
 */

package fvb;

import java.awt.AWTException;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JWindow;
import javax.swing.UIManager;

/**
 *
 * @author  Bageshwar
 */
public class Settings extends javax.swing.JFrame implements Serializable{
    
    Bot bot=null;
    
    /** Creates new form Settings */
    public Settings() {
        super();
        /*
        UIManager.LookAndFeelInfo plaf[] = UIManager.getInstalledLookAndFeels();
        for (int i = 0; i < plaf.length; i++) {
            System.out.println(plaf[i].getClassName());
        }
         */
        try {
            /*
            javax.swing.plaf.metal.MetalLookAndFeel
            com.sun.java.swing.plaf.motif.MotifLookAndFeel
            com.sun.java.swing.plaf.windows.WindowsLookAndFeel
            com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel
             
             */
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {
            System.out.println("Could not change the look and feel of the application");
            e.printStackTrace();
        }
        initComponents();
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
    private void initComponents() {
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        delay = new javax.swing.JTextField();
        angle = new javax.swing.JTextField();
        constant = new javax.swing.JTextField();
        gridY = new javax.swing.JTextField();
        gridX = new javax.swing.JTextField();
        startY = new javax.swing.JTextField();
        startX = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        clickDelay = new javax.swing.JTextField();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        beans = new javax.swing.JTextField();
        jButton12 = new javax.swing.JButton();
        jButton13 = new javax.swing.JButton();
        jButton14 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("FarmVille Bot [Contact me at hunnynarain@gmail.com]");
        setLocationByPlatform(true);
        setResizable(false);
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
                formWindowLostFocus(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                formMouseMoved(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jButton1.setText("Start FVB");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jLabel1.setText("StartY");

        jLabel2.setText("Delay");

        jLabel3.setText("StartX");

        jLabel4.setText("Constant");

        jLabel5.setText("Angle");

        jLabel6.setText("GridY");

        jLabel7.setText("GridX");

        delay.setColumns(5);
        delay.setText("5");

        angle.setColumns(5);
        angle.setText("26.000");

        constant.setColumns(5);
        constant.setText("26");

        gridY.setColumns(5);
        gridY.setText("5");

        gridX.setColumns(5);
        gridX.setText("10");

        startY.setColumns(5);
        startY.setText("200");

        startX.setColumns(5);
        startX.setText("500");

        jButton2.setText("Capture Screen");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jButton3.setText("Select Start coordinates");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        jButton4.setText("Select Delete Tool");
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton4MouseClicked(evt);
            }
        });

        jButton5.setText("Select Crop");
        jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton5MouseClicked(evt);
            }
        });

        jButton6.setText("Select Delete Accept");
        jButton6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton6MouseClicked(evt);
            }
        });

        jButton7.setFont(new java.awt.Font("Tahoma", 1, 12));
        jButton7.setText("Start Deleting Field !!!");
        jButton7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton7MouseClicked(evt);
            }
        });

        jLabel8.setText("Click");

        clickDelay.setText("50");

        jButton8.setText(" Select Market");
        jButton8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton8MouseClicked(evt);
            }
        });

        jButton9.setText("HELP");
        jButton9.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton9MouseClicked(evt);
            }
        });
        jButton9.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jButton9KeyPressed(evt);
            }
        });

        jButton10.setFont(new java.awt.Font("Tahoma", 1, 12));
        jButton10.setText("SoyaBean Hack");
        jButton10.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton10MouseClicked(evt);
            }
        });

        jButton11.setText("Select Plow Tool");
        jButton11.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton11MouseClicked(evt);
            }
        });

        jLabel9.setText("No of Beans");
        jLabel9.setToolTipText("Check your coins. You will get 3 XP for every 30 coins, so calculate likewise and enter a suitable number.");

        beans.setText("100");
        beans.setToolTipText("Check your coins. You will get 3 XP for every 30 coins, so calculate likewise and enter a suitable number.");

        jButton12.setText("Select Plow Point");
        jButton12.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton12MouseClicked(evt);
            }
        });

        jButton13.setText("Save Settings");
        jButton13.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton13MouseClicked(evt);
            }
        });

        jButton14.setText("Load Settings");
        jButton14.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton14MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel8))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel9)))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(beans, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                    .addComponent(clickDelay, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                    .addComponent(delay, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                    .addComponent(startX, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                    .addComponent(startY, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                    .addComponent(gridX, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                    .addComponent(gridY, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                    .addComponent(angle, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                    .addComponent(constant, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                    .addComponent(jButton8, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                    .addComponent(jButton6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton11, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                            .addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                            .addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                            .addComponent(jButton10, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)))))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jButton9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton14)
                .addGap(261, 261, 261))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(delay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(startX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(startY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(gridX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(gridY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(constant, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(angle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(clickDelay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(beans, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton12)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton10)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton9)
                    .addComponent(jButton13)
                    .addComponent(jButton14)))
        );
        pack();
    }// </editor-fold>                        
    
    private void formMouseMoved(java.awt.event.MouseEvent evt) {                                
// TODO add your handling code here:
    }                               
    
    private void formWindowLostFocus(java.awt.event.WindowEvent evt) {                                     
// TODO add your handling code here:
        System.out.println("window lost focus");
        try {
            Bot.run = true;
        } catch (NullPointerException e) {
            System.out.println("Was trying to set parameter an Bot which has not been initialed yet.");
        }
    }                                    
    
    private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {                                       
// TODO add your handling code here:
        System.out.println("window gained focus");
        try{
            Bot.run = false;
            System.out.println("sleeping");
            Thread.sleep(1000);
            System.out.println("wakeup");
        } catch (Exception e) {
            System.out.println("Was trying to set parameter an Bot which has not been initialed yet.");
        }
    }                                      
    
    private void jButton14MouseClicked(java.awt.event.MouseEvent evt) {                                       
// TODO add your handling code here:
        //try to deserialize the settings.
        
        
        try {
            JFileChooser filec = new JFileChooser();
            filec.setCurrentDirectory(new File(System.getProperty("user.dir")));
            //filec.addChoosableFileFilter(filter);
            int response = filec.showDialog(this,"Save Settings");
            
            if(response==JFileChooser.APPROVE_OPTION){
                String filename = filec.getSelectedFile().getAbsolutePath();
                FileInputStream fin = new FileInputStream(filename);
                ObjectInputStream oin = new ObjectInputStream(fin);
                Object ob = oin.readObject();
                Persist p = (Persist)ob;
                
                //now extract values from persist and set the values into the current form.
                this.deleteTool = (Point)p.map.get("deleteTool");
                this.deleteAccept = (Point)p.map.get("deleteAccept");
                this.marketSelect = (Point)p.map.get("marketSelect");
                this.cropSelect = (Point)p.map.get("cropSelect");
                this.plowSelect = (Point)p.map.get("plowSelect");
                this.plowPointSelect = (Point)p.map.get("plowPointSelect");
                
                this.startX.setText((String)p.map.get("startX"));
                this.startY.setText((String)p.map.get("startY"));
                this.delay.setText((String)p.map.get("delay"));
                this.clickDelay.setText((String)p.map.get("clickDelay"));
                this.gridX.setText((String)p.map.get("gridX"));
                this.gridY.setText((String)p.map.get("gridY"));
                this.beans.setText((String)p.map.get("beans"));
                this.constant.setText((String)p.map.get("constant"));
                this.angle.setText((String)p.map.get("angle"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }                                      
    
    private void jButton13MouseClicked(java.awt.event.MouseEvent evt) {                                       
// TODO add your handling code here:
        //need to serialize the settings.
        try {
            
            //push things into the hashmap.
            //6 coordinates
            Persist p = new Persist();
            p.map.put("deleteTool",this.deleteTool);
            p.map.put("marketSelect",this.marketSelect);
            p.map.put("plowSelect",this.plowSelect);
            p.map.put("plowPointSelect",this.plowPointSelect);
            p.map.put("cropSelect",this.cropSelect);
            p.map.put("deleteAccept",this.deleteAccept);
            
            //9 text parameters
            p.map.put("delay",this.delay.getText());
            p.map.put("startX",this.startX.getText());
            p.map.put("startY",this.startY.getText());
            p.map.put("gridX",this.gridX.getText());
            p.map.put("gridY",this.gridY.getText());
            p.map.put("constant",this.constant.getText());
            p.map.put("angle",this.angle.getText());
            p.map.put("clickDelay",this.clickDelay.getText());
            p.map.put("beans",this.beans.getText());
            //p.map.put("",this);
            
            //FileNameExtensionFilter  filter  = new FileNameExtensionFilter("Saved Settings",new String[]{"save","settings"});
            
            
            JFileChooser filec = new JFileChooser();
            filec.setCurrentDirectory(new File(System.getProperty("user.dir")));
            //filec.addChoosableFileFilter(filter);
            int response = filec.showDialog(this,"Save Settings");
            
            if(response==JFileChooser.APPROVE_OPTION){
                String filename = filec.getSelectedFile().getAbsolutePath();
                System.out.println("Saving the settings by the name "+filename);
                
                FileOutputStream fs = new FileOutputStream(filename);
                ObjectOutputStream oos = new ObjectOutputStream(fs);
                oos.writeObject(p);
                oos.close();
                fs.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        
    }                                      
    
    
    private void jButton12MouseClicked(java.awt.event.MouseEvent evt) {                                       
// TODO add your handling code here:
        
        //here the point where the plow should be is recorded, this is a little typical
        win.type = "plowPointSelect";
        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(win);
        
    }                                      
    
    private void jButton11MouseClicked(java.awt.event.MouseEvent evt) {                                       
// TODO add your handling code here:
        
        //select Plow Tool
        win.type = "plowSelect";
        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(win);
    }                                      
    /**
     * Pass in the numbe of pure seconds needed for the process.
     * This method will add the number of seconds to wait before starting the bot
     * to itself,since that is common for each operation.
     * */
    private String getTimeDuration(long seconds){
        long minutes=0,hours=0;
        String r="";
        try {
            seconds+=Long.parseLong(this.delay.getText());
        } catch (Exception e) {
            System.out.println("Could not fetch the delay value while formatting TimeDurationString.Taking it as 0");
        }
        
        if(seconds>60)
            minutes = seconds/60;
        
        if(minutes>60)
            hours = minutes/60;
        
        
        seconds = seconds%60;
        minutes = minutes%60;
        
        if(hours!=0)
            r+=hours+ " Hours ";
        if(minutes!=0)
            r+=minutes+" Minutes ";
        r+=seconds +" Seconds.";
        return r;
    }
    
    private void jButton10MouseClicked(java.awt.event.MouseEvent evt) {                                       
// TODO add your handling code here:
        //start the bot to do soyabeanhack
        try {
            //total delay in seconds
            long totalDelay = BotAction.SOYABEAN_HACK.getLoopSize()*
                    Integer.parseInt(this.beans.getText())*Integer.parseInt(this.clickDelay.getText())/1000;
            int resp=JOptionPane.showConfirmDialog(this,"This action of yours will take approximately "+this.getTimeDuration(totalDelay));
            this.setTitle("Enter the Settings");
            if(resp==JOptionPane.OK_OPTION)
            new Thread(new Runnable(){
                public void run(){
                    try {
                        bot  = new Bot(delay.getText(),angle.getText(),startX.getText(),startY.getText(),
                                gridX.getText(),gridY.getText(),constant.getText(),clickDelay.getText(),beans.getText(),
                                deleteTool,deleteAccept,cropSelect,plowSelect,plowPointSelect,marketSelect
                                );
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }).start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        this.setTitle("Done with the Accelerated Farming stuff");
    }                                      
    
    private void jButton9MouseClicked(java.awt.event.MouseEvent evt) {                                      
// TODO add your handling code here:
        Help help = new Help();
        help.setTitle("Help for FarmVille Bot [contact me at hunnynarain@gmail.com]");
        help.setVisible(true);
    }                                     
    
    private void jButton9KeyPressed(java.awt.event.KeyEvent evt) {                                    
// TODO add your handling code here:
    }                                   
    
    private void jButton8MouseClicked(java.awt.event.MouseEvent evt) {                                      
// TODO add your handling code here:
        //select the market
        win.type = "marketSelect";
        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(win);
    }                                     
    
    private void jButton7MouseClicked(java.awt.event.MouseEvent evt) {                                      
// TODO add your handling code here:
        
        //start the bot to perform IT !!!
        
        try {
            
            //total delay in seconds
            long totalDelay = BotAction.DELETE_FIELD.getLoopSize()*
                    Integer.parseInt(this.gridX.getText())*Integer.parseInt(this.gridY.getText())*Integer.parseInt(this.clickDelay.getText())/1000;
            int resp = JOptionPane.showConfirmDialog(this,"This action of yours will take approximately "+this.getTimeDuration(totalDelay));
            this.setTitle("Enter the Settings");
            if(resp==JOptionPane.OK_OPTION)
            new Thread(new Runnable(){
                public void run(){
                    try {
                        bot  = new Bot(delay.getText(),angle.getText(),startX.getText(),startY.getText(),
                                gridX.getText(),gridY.getText(),constant.getText(),clickDelay.getText(),
                                deleteTool,deleteAccept
                                );
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }).start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        this.setTitle("Done with the Accelerated Farming stuff");
    }                                     
    
    private void jButton6MouseClicked(java.awt.event.MouseEvent evt) {                                      
// TODO add your handling code here:
        //select the delete accept dialog
        win.type = "deleteAccept";
        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(win);
    }                                     
    
    private void jButton5MouseClicked(java.awt.event.MouseEvent evt) {                                      
// TODO add your handling code here:
        //select crop and the next button
        win.type = "cropSelect";
        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(win);
    }                                     
    
    private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {                                      
// TODO add your handling code here:
                     
        //to select delete tool
        win.type = "deleteTool";
        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(win);
    }                                     
    
    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {                                      
// TODO add your handling code here:
        //this.setVisible(false);
        win.type = "start";
        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(win);
        //  this.jPanel1.add(win);
        
    }                                     
    
    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {                                      
// TODO add your handling code here:
        
        //this.setVisible(false);
        this.setTitle("FarmVille Bot [Contact me at hunnynarain@gmail.com]");
        win  = new Win(this);
        try {
            int r = JOptionPane.showConfirmDialog(this,"Make sure that FarmVille Screen is ready.\nThe delay is set to "+this.delay.getText()+"" +
                    " seconds.Are you sure you will be able to switch to FarmVille and perform the " +
                    "necessary actions before FVB captures the screen.");
            if(r==JOptionPane.OK_OPTION){
                win.goFullScreen(Integer.parseInt(delay.getText())*1000  );
                this.setTitle("Screenshot Taken...");
                this.requestFocus();
                JOptionPane.showMessageDialog(this,"Screen has been captured successfully","FVB : Screen Capture successful",JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        
    }                                     
    
    private void formMouseClicked(java.awt.event.MouseEvent evt) {                                  
// TODO add your handling code here:
        try {
            GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }                                 
    
    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        try {
// TODO add your handling code here:
            //total delay in seconds
            long totalDelay = BotAction.TRAVERSE.getLoopSize()*
                    Integer.parseInt(this.gridX.getText())*Integer.parseInt(this.gridY.getText())*Integer.parseInt(this.clickDelay.getText())/1000;
            int resp = JOptionPane.showConfirmDialog(this,"This action of yours will take approximately "+this.getTimeDuration(totalDelay));
            this.setTitle("Enter the Settings");
            if(resp==JOptionPane.OK_OPTION)
            new Thread(new Runnable(){
                public void run(){
                    try {
                        bot  = new Bot(delay.getText(),angle.getText(),startX.getText(),startY.getText(),
                                gridX.getText(),gridY.getText(),constant.getText(), clickDelay.getText());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }).start();
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        this.setTitle("Done with the Farming stuff");
        /*JDialog jd = new JDialog(this,"Message",true);
        jd.setVisible(true);*/
    }                                     
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Settings().setVisible(true);
            }
            
        });
        
    }
    Win win,marketwin;
    
    // Variables declaration - do not modify                     
    private javax.swing.JTextField angle;
    private javax.swing.JTextField beans;
    private javax.swing.JTextField clickDelay;
    private javax.swing.JTextField constant;
    private javax.swing.JTextField delay;
    private javax.swing.JTextField gridX;
    private javax.swing.JTextField gridY;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton13;
    private javax.swing.JButton jButton14;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField startX;
    private javax.swing.JTextField startY;
    // End of variables declaration                   
    
    public void setXY(String type,int x,int y) {
        //if this call was for startx and starty coordinates
        Point p = null;
        if(type.equalsIgnoreCase("start")){
            this.startX.setText(x+"");
            this.startY.setText(y+"");
        }else p = new java.awt.Point(x,y);
        
        if(type.equalsIgnoreCase("deleteTool"))
            deleteTool  = p;
        
        if(type.equalsIgnoreCase("cropSelect"))
            cropSelect = p;
        
        if(type.equalsIgnoreCase("deleteAccept"))
            deleteAccept = p;
        
        if(type.equalsIgnoreCase("marketSelect"))
            marketSelect = p;
        
        if(type.equalsIgnoreCase("plowSelect"))
            plowSelect = p;
        
        if(type.equalsIgnoreCase("plowPointSelect"))
            plowPointSelect = p;
        
        
    }
    
    java.awt.Point deleteTool,cropSelect,deleteAccept,marketSelect,plowSelect,plowPointSelect;
}


class O implements ImageObserver {
    public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
        return true;
    }
    
}

class Win extends JWindow {
    BufferedImage im;
    JLabel imgHolder;
    Settings parent;
    String filename = "img.png";
    public String type = "start";
    
    public Win(Settings frm) {
        super();
        parent = frm;
        
        addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent me){
                System.out.println(me.getLocationOnScreen());
                parent.setXY(type,me.getLocationOnScreen().y,me.getLocationOnScreen().x);
                GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(null);
                parent.setVisible(true);
                dispose();
                JOptionPane.showMessageDialog(parent,"The coordinate "+me.getLocationOnScreen().toString()+" " +
                        "has been selected.","Point Selected",JOptionPane.INFORMATION_MESSAGE);
            }
        });
        
        addMouseMotionListener(new MouseMotionAdapter(){
            @Override
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                mouseMoving(evt);
            }
            
        });
        
    }
    public Win(Settings frm,String file){
        this(frm);
        this.filename = file;
    }
    
    private void mouseMoving(MouseEvent evt){
        this.repaint();
        this.getGraphics().drawString(evt.getLocationOnScreen().toString(),200,200);
        //System.out.println(evt.getLocationOnScreen());
    }
    
    /*public void paint(Graphics g){
        //super.paint(g);
        //g.drawRect(0,0,200,300);
        g.drawImage(im,0,0,null);
     
    }*/
    
    private JLabel getImgHolder(){
        String dir = System.getProperty("user.dir");
        String fs =System.getProperty("file.separator");
        return imgHolder = new JLabel(new ImageIcon(dir+fs+this.filename)) ;
    }
    
    public void goFullScreen(int delay) throws InterruptedException, AWTException, IOException{
        Thread.sleep(delay);
        Robot rb = new Robot();
        int w = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode().getWidth();
        int h = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode().getHeight();
        im = rb.createScreenCapture(new Rectangle(0,0,w,h));
        ImageIO.write(im,"png",new File(this.filename));
        this.add(getImgHolder());
        this.pack();
    }
    
    
}